package net.wanho.servlet;

import com.alibaba.fastjson.JSON;
import net.wanho.entity.Account;
import net.wanho.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Author：汤小洋
 * Date：2025-09-05 10:18
 * Description：<描述>
 */
@WebServlet("/login")
public class AjaxServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");


        /**
         * 允许跨域访问
         */
        resp.setHeader("Access-Control-Allow-Origin", "*"); // 允许所有域名访问

        resp.setContentType("text/plain; charset=utf-8");
        PrintWriter out = resp.getWriter();


        if ("admin".equals(username) && "123".equals(password)){
            Account account = new Account(1, username, password, "张三", 18, new Date());
            out.print(JSON.toJSONString(AjaxResult.success(200, "登录成功", account)));
        }else{
           out.print(JSON.toJSONString(AjaxResult.fail(500, "登录失败")));
        }
        out.close();
    }
}
